package com.sykj.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sykj.bean.User;
import com.sykj.service.UserService;
import com.sykj.util.PointProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: spirngboot_Demo
 * @description:
 * @author: qiaoli
 * @create: 2020-07-11 09:45
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    PointProducer pointProducer;
@RequestMapping("/add")
    public User  adduser(User user){
        boolean save = userService.save(user);
        if(save==true){
           redisTemplate.opsForValue().set("sysytem:user:"+user.getId(),user);
            User o = (User) redisTemplate.opsForValue().get("sysytem:user:" + user.getId());
            System.err.println(o.toString());
            pointProducer.send(user.getUsername());
            return user;
        }else
            return new User();
    }
    @RequestMapping("/login")
    public  String  login(){
    return "请先登录";
    }



    @RequestMapping("/queryList")
    public List<User> queryUsers(User user){
        List<User> list = userService.list();
        return list;
    }
    @RequestMapping("/sava")
    public boolean  savaUser(@PathVariable(value = "user",required = false ) User user){
        boolean save = userService.save(user);
        return save;
    }
    @RequestMapping("/delete/{id}")
    public boolean  delete(@PathVariable(value = "id")Integer id){
        boolean save = userService.removeById(id);
        return save;
    }
    @RequestMapping("/update")
    public  boolean    update(User user){
        boolean b = userService.updateById(user);
        return b;
    }
    @RequestMapping("/selectByid")
    public User  selectByid(Integer id){
        User byId = userService.getById(id);
        return byId;
    }
    @RequestMapping("/queryBy")
    public List<User> queryAllby(User user){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        System.err.println(user.toString());
        if(user.getStatus()!=null && user.getStatus()!="" ){
            queryWrapper.eq("status",user.getStatus());
        }
        if(user.getUsername()!=null && user.getUsername()!="" ){
            queryWrapper.like("username",user.getUsername());
        }
        if(user.getPassword()!=null && user.getPassword()!="" ){
            queryWrapper.eq("password",user.getPassword());
        }

        List<User> list = userService.list(queryWrapper);
        return list;
    }
}
